package com.mind.adjust.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.mind.adjust.mapper.AjAttachCateBusinessMapper;
import com.mind.api.serivce.ShareServiceImpl;
import com.mind.cache.lock.annotation.DMLock;
import com.mind.cache.service.CacheService;
import com.mind.customer.mapper.AjTicketTypeMapper;
import com.mind.system.domain.AjAttachCateBusiness;
import com.mind.system.domain.AjTicketType;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Component
@Log4j2
public class SyncService {
    @Resource
    private ShareServiceImpl shareService;
    @Resource
    private AjTicketTypeMapper ticketTypeMapper;
    @Resource
    private AjAttachCateBusinessMapper attachCateBusinessMapper;

    @Resource
    private CacheService cacheService;
    public void syncTicketType(){
        JSONArray ticketTypes = shareService.getTicketType();
        Set<String> existsOriginalId = ticketTypeMapper.selectAllOriginalId();
        for(int i = 0; i<ticketTypes.size();i++){
            JSONObject object = ticketTypes.getJSONObject(i);
            AjTicketType ticketType = new AjTicketType();
            if(existsOriginalId.contains(object.getString("docTypeId"))){
                ticketType.setCode(object.getString("docTypeCode"));
                ticketType.setOriginalId(object.getString("docTypeId"));
                ticketType.setName(object.getString("docTypeName"));
                ticketTypeMapper.updateTicketTypeByOriginalId(ticketType);
            }else{

                ticketType = new AjTicketType(){{
                    setCode(object.getString("docTypeCode"));
                    setOriginalId(object.getString("docTypeId"));
                    setName(object.getString("docTypeName"));
                }};
                ticketTypeMapper.insertTicketType(ticketType);
            }
            existsOriginalId.remove(object.getString("docTypeId"));
        }
        for (String originalId : existsOriginalId){
            ticketTypeMapper.deleteByOriginalId(originalId);
        }
    }

    public void syncBusinessCate(){
       Set<String> existsOriginalId = attachCateBusinessMapper.selectAllOriginalId();
        JSONArray ticketTypes = shareService.getBusinessCate();
        Map<String,String> mainCate = new HashMap<>();
        Map<String, String> subCate = new HashMap<>();
        for(int i = 0; i<ticketTypes.size();i++){
            JSONObject object = ticketTypes.getJSONObject(i);
            AjAttachCateBusiness attachCateBusiness = new AjAttachCateBusiness();
            attachCateBusiness.setOriginalId(object.getString("shortCode"));
            attachCateBusiness.setCode(object.getString("mainShortCode"));
            attachCateBusiness.setName(object.getString("mainDescription"));
            attachCateBusiness.setSmallCode(object.getString("shortCode"));
            attachCateBusiness.setSmallName(object.getString("description"));
            attachCateBusiness.setModuleName(object.getString("moduleName"));
            attachCateBusiness.setTicketTypeName(object.getString("docTypeName"));
            attachCateBusiness.setTicketTypeCode(object.getString("docTypeCode"));
            attachCateBusiness.setType("业务大小类");
            if(existsOriginalId.contains(object.getString("shortCode"))){
                attachCateBusinessMapper.updateAttachCateBusinessByOriginalId(attachCateBusiness);
            }else{
                attachCateBusinessMapper.insertAttachCateBusiness(attachCateBusiness);
            }
            existsOriginalId.remove(object.getString("shortCode"));
            if(!mainCate.containsKey(object.getString("mainShortCode"))){
                mainCate.put(object.getString("mainShortCode"),object.getString("mainDescription"));
            }

            if(!subCate.containsKey(object.getString("shortCode"))){
                subCate.put(object.getString("shortCode"),object.getString("description"));
            }
        }
        for (String originalId : existsOriginalId){
            attachCateBusinessMapper.deleteByOriginalId(originalId);
        }
        cacheService.deleteObject("mainCateCache");
        cacheService.deleteObject("subCateCache");
        cacheService.setCacheObject("mainCateCache",mainCate);
        cacheService.setCacheObject("subCateCache",subCate);

    }
    @DMLock
    public void sync(String key){
        log.info("开始同步数据...");
        try {
           syncTicketType();
        } catch (Exception e) {
            log.error("同步单据类型失败：", e);
        }
        try {
            syncBusinessCate();
        } catch (Exception e) {
            log.error("同步业务数据失败：", e);
        }
        log.info("同步数据完成...");
    }
}
